473,419 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Dynamic Collapsible Tables for any number of tables

1
I'm newbie to javascript and I did an extensive search on this site and couple of others looking for directions.
I'm attempting to write some custom javascript for collapsing the tables and fit it in an existing application already provided. I found some code on the web that works great. In this example there are 3 fixed set of tables with 3 fixed/hardcoded "id".
I tried to fit this code into a FOR loop so it becomes dynamic and it would work for any number and not just 3 tables.

Expand|Select|Wrap|Line Numbers
  1. FOR (f, f>0, f++)
  2. <td style="background-color: #CCC"><a href="#" onclick="return toggleItem('collapse_myTbody1[f]')">Toggle 1</a></td></tr>
  3.  
But it is not working.
Has anyone done this before. Can anyone share some ideas or pointers?

Regards,
MHT
NY City.


[HTML]<html>
<head>
<title>My Doc</title>
<script language="javascript">
function getItem(id)
{
var itm = false;
if(document.getElementById)
itm = document.getElementById(id);
else if(document.all)
itm = document.all[id];
else if(document.layers)
itm = document.layers[id];

return itm;
}

function toggleAll(dowhat)
{
var tags = document.getElementsByTagName('tbody');
if(!tags)
return false;

for(var i = 0; i < tags.length; i++)
{
if(tags[i].className == 'collapse_obj')
{
if(dowhat == 'collapse')
tags[i].style.display = 'none';
else
tags[i].style.display = '';
}
}

return false;
}

function toggleItem(id)
{
itm = getItem(id);

if(!itm)
return false;

if(itm.style.display == 'none')
itm.style.display = '';
else
itm.style.display = 'none';

return false;
}
</script>
</head>
<body>

<a href="#" onclick="return toggleAll('collapse')">Collapse All</a>
<a href="#" onclick="return toggleAll('expand')">Expand All</a>

<table width="400">
<tbody>
<tr><td style="background-color: #CCC"><a href="#" onclick="return toggleItem('collapse_myTbody1')">Toggle 1</a></td></tr>
</tbody>

<tbody class="collapse_obj" id="collapse_myTbody1">
<tr><td>Test row1</td></tr>
<tr><td>Test row2</td></tr>
<tr><td>Test row3</td></tr>
<tr><td>Test row4</td></tr>
<tr><td>Test row5</td></tr>
</tbody>
</table>

<table width="400">
<tbody>
<tr><td style="background-color: #CCC"><a href="#" onclick="return toggleItem('collapse_myTbody2')">Toggle 2</a></td></tr>
</tbody>

<tbody class="collapse_obj" id="collapse_myTbody2">
<tr><td>Test row1</td></tr>
<tr><td>Test row2</td></tr>
<tr><td>Test row3</td></tr>
<tr><td>Test row4</td></tr>
<tr><td>Test row5</td></tr>
</tbody>
</table>

<table width="400">
<tbody>
<tr><td style="background-color: #CCC"><a href="#" onclick="return toggleItem('collapse_myTbody3')">Toggle 3</a></td></tr>
</tbody>

<tbody class="collapse_obj" id="collapse_myTbody3">
<tr><td>Test row1</td></tr>
<tr><td>Test row2</td></tr>
<tr><td>Test row3</td></tr>
<tr><td>Test row4</td></tr>
<tr><td>Test row5</td></tr>
</tbody>
</table>

</body>
</html>[/HTML]
Aug 30 '06 #1
1 7701
acoder
16,027 Expert Mod 8TB
Either use innerHTML, e.g.
Expand|Select|Wrap|Line Numbers
  1. for (f=0, f<max, f++) {
  2.     tableObj.innerHTML+='<td style="background-color: #CCC"><a href="#" onclick="return toggleItem(\'collapse_myTbody'+f+'\')">Toggle 1</a></td></tr>';
  3.  
or use the DOM methods, e.g.
Expand|Select|Wrap|Line Numbers
  1. for (f = 0; f < max; f++) {
  2.     var table = document.createElement("table");
  3.     var tbody = document.createElement("tbody");
  4.     ...
  5.     td.style.backgroundColor = "#CCC";
  6.     ...
  7.     tbody.appendChild(tr);
  8.     table.appendChild(tbody);
See this link for more information.
May 13 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bootstrap bill | last post by:
I'm looking to create collapsible tables, similar to the outlines available in Microsoft Excel and Open Office spreadsheet. I'd prefer something cross platform. Is something like this available...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
2
by: geodev | last post by:
Hello, I’m currently writing an asp.net application that will be running on a Windows XP Professional workstation utilising IIS and MSDE Database. At a later date this application will need to...
0
by: mj2736 | last post by:
I have a pair of database tables with a structure similar to the following: -------------------------------------------- Table: Category -------------------------------------------- CategoryID...
5
by: alingsjtu | last post by:
Hello, every body. When execute dynamic generated multiple OPENQUERY statements (which linkes to DB2) in SQLServer, I always got SQL1040N The maximum number of applications is already connected...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
9
by: pbd22 | last post by:
Hi. This is just a disaster management question. I am using XMLHTTP for the dynamic loading of content in a very crucial area of my web site. Same as an IFrame, but using XMLHTTP and a DIV. I...
1
by: mahesh123 | last post by:
I am using Collapsible Panel in my project.But in other form of my project i want to use collapsible panel inside other Collapsible panel.Is it possible. Both panels are to be dinamic.Is it possible...
0
JamieHowarth0
by: JamieHowarth0 | last post by:
I'm about to set up a new website, powered by DotNetNuke (my favourite CMS at the moment, mainly because it's free), and I want to install a Counter-Strike game server onto the same machine. I also...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.